/**
 * Created by lvlq on 16/3/13.
 */
var Q = require("q");

module.exports.index = function (req, res) {
    var ids = req.query.prize.split(",");
    var Prize = M("index.prize");
    var prize_word = [];

    var sum = 0;
    for (var i = 0; i < ids.length; i++) {
        sum += parseInt(ids[i]);
    }

    var class_arr = ["", "", ""];

    switch (sum) {
        case 1:
            class_arr[0] = "p-1";
            break;
        case 2:
            class_arr[1] = "p-1";
            break;
        case 3:
            if (ids.length == 1) {
                class_arr[2] = "p-1";
            } else {
                class_arr[1] = "p-1";
                class_arr[0] = "p-2";
            }
            break;
        case 4:
            class_arr[2] = "p-1";
            class_arr[0] = "p-2";
            break;
        case 5:
            class_arr[2] = "p-1";
            class_arr[1] = "p-2";
            break;
        case 6:
            class_arr[2] = "p-1";
            class_arr[1] = "p-2";
            class_arr[0] = "p-3";
            break;
    }

    var Result = M("index.result");
    var submit = 1;
    Result.find({
        where: {
            openid: req.query.openid,
            pid: {
                "in": ids
            }
        }
    }).then(function (data) {
        if (data) {
            submit = 0;
        }
        return Prize.findAll({
            where: {
                id: {
                    "in": ids
                }
            }
        })
    }).then(function (data) {
        if (data.length == 0) {
            throw new Error("系统错误");
        }

        if (data.length == 1) {
            prize_word[0] = "";
            prize_word[1] = data[0].name;
        } else if (data.length == 2) {
            prize_word[0] = data[0].name;
            prize_word[1] = data[1].name;
        } else {
            prize_word[0] = data[0].name + "、" + data[1].name;
            prize_word[1] = data[2].name;
        }

        res.render("contact.html", {
            prize_word: prize_word,
            class_arr: class_arr,
            submit: submit
        });
    }).catch(function () {
        res.send("系统错误,请联系管理员~~");
    });

};


module.exports.saveinfo = function (req, res) {
    var pids = req.body.pids.split(",");
    var name = req.body.name;
    var phone = req.body.phone;
    var openid = req.body.openid;
    var Result = M("index.result");
    var User = M("index.user");

    User.find({
        where: {
            openid: openid
        }
    }).then(function (u) {
        dosave(pids, name, phone, u, 0, function (err) {
            if (err) {
                console.log(err.message);
                res.json({
                    errno: 1001,
                    errmsg: "系统错误"
                });
            } else {
                res.json({
                    errno: 0
                });
            }
        })
    });
};

var dosave = function (pids, name, phone, user, index, cb) {
    if (index < pids.length) {
        save(pids[index], name, phone, user).then(function () {
            index++;
            dosave(pids, name, phone, user, index, cb);
        }).catch(function (err) {
            cb(err);
        });
    } else {
        cb(null)
    }

};


var save = function (pid, name, phone, user) {
    var defer = Q.defer();

    var Prize = M("index.prize");
    var Result = M("index.result");
    var prize_name;
    Prize.find({
        where: {
            id: pid
        }
    }).then(function (prize) {
        if (!prize) throw new Error("系统错误");
        prize_name = prize.name;
        return Result.find({
            where: {
                openid: user.openid,
                pid: pid
            }
        })

    }).then(function (data) {
        if (data) return {};
        else {
            return Result.create({
                openid: user.openid,
                nickname: user.nickname,
                headimgurl: user.headimgurl,
                pid: pid,
                pname: prize_name,
                name: name,
                phone: phone
            });
        }
    }).then(function () {
        defer.resolve();
    }).catch(function (err) {
        defer.reject(err);
    });

    return defer.promise;
};